/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.nodes;

import com.googlecode.ivybeans.module.IvyLibraryController;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.event.ChangeListener;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.ui.support.NodeFactory;
import org.netbeans.spi.project.ui.support.NodeList;
import org.openide.nodes.Node;
import org.openide.util.ChangeSupport;

/**
 * 
 * @author lforet
 */
public class IvyNodeFactory implements NodeFactory {

    public NodeList createNodes(Project p) {
        return new IvyLibraryNodeList(p);
    }

    private static class IvyLibraryNodeList implements NodeList<String>, PropertyChangeListener {

        private final static String IVY_LIBRAIRIES = "ivyLibs";
        private final static String IVY_FILES = "ivyFiles";
        private final ChangeSupport changeSupport = new ChangeSupport(this);
        private final Project project;
        private final IvyLibraryController ivyLibraryController;
        private List<String> keys;

        public IvyLibraryNodeList(Project p) {
            project = p;
            ivyLibraryController = project.getLookup().lookup(IvyLibraryController.class);
            ivyLibraryController.addPropertyChangeListener(this);
            setKeys();
        }

        public void addChangeListener(ChangeListener l) {
            changeSupport.addChangeListener(l);
        }

        public void addNotify() {
        }

        public List<String> keys() {
            return keys;
        }

        public Node node(String key) {
            if (IVY_LIBRAIRIES.equals(key)) {
                return new LibrariesNode(project);
            } 
            if (IVY_FILES.equals(key)) {
                return new IvyFilesNode(project);
            } 
            return null;
        }

        public void removeChangeListener(ChangeListener l) {
            changeSupport.removeChangeListener(l);
        }

        public void removeNotify() {
        }

        public void propertyChange(PropertyChangeEvent evt) {
            if (IvyLibraryController.IVY_LIBRARY_RESOLVED.equals(evt.getPropertyName())) {
                setKeys();
                changeSupport.fireChange();
            }
        }

        private void setKeys() {
            if (ivyLibraryController.isIvyEnabled()) {
                keys = new ArrayList<String>();
                keys.add(IVY_LIBRAIRIES);
                keys.add(IVY_FILES);
            } else {
                keys = Collections.emptyList();
            }
        }
    }
}
